#include "LOGR_simple_pages.h"
#include "LOGR_independent_pages.h"

char *HTGetMime(char *url);

WebSimplePagePtr
new_web_simple_page_node(void)
{
  WebSimplePagePtr spn = (WebSimplePagePtr)calloc(1, sizeof(WebSimplePageStruct));
  if(!spn) {
      fprintf(stderr,"Out of Memory new_web_simple_page_node\n");
      abort();
  }
  return spn;
}

void
delete_web_simple_page_node(WebSimplePagePtr spn)
{	
    if(!spn) return;
    free(spn); spn = NULL;
}

void
LOGR_set_type_simple_page(WebSimplePagePtr spn,TypePage tp)
{
  if(!spn) return;
  spn->tp = tp;
}

TypePage
LOGR_get_type_simple_page(WebSimplePagePtr spn)
{
  if(!spn) return 0;
  return spn->tp;
}

void
LOGR_set_info_simple_page(WebSimplePagePtr spn,void *np)
{
  if(!spn) return;
  spn->nodeptr = np;
}

void *
LOGR_get_info_simple_page(WebSimplePagePtr spn)
{
  if(!spn) return NULL;
  return spn->nodeptr;
}

int
LOGR_is_access_simple_lower(WebSimplePagePtr u, WebSimplePagePtr v)
{
    long nu = u ? LOGR_get_simple_naccesses(u) : 0;
    long nv = v ? LOGR_get_simple_naccesses(v) : 0;
    return (nu < nv);
}

int
LOGR_is_access_simple_bigger(WebSimplePagePtr u,WebSimplePagePtr v)
{
    long nu = u ? LOGR_get_simple_naccesses(u) : 0;
    long nv = v ? LOGR_get_simple_naccesses(v) : 0;
    return (nu > nv);
}

const char *
LOGR_get_simple_url(WebSimplePagePtr spn)
{
    if(!spn)
	return NULL;
    if(spn->tp == PWEBNODE)
    {
	PNODE v = (PNODE)spn->nodeptr;
	return  v ? get_label_vertex(v) : NULL;
    }
    else if(spn->tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)spn->nodeptr;
	return opn ? LOGR_get_ip_url(opn) : NULL;
    }
    return NULL;
}

long
LOGR_get_simple_sizebytes(WebSimplePagePtr spn)
{
    if(!spn)
	return -1;

    if(spn->tp == PWEBNODE)
    {
	PNODE v = (PNODE)spn->nodeptr;
	char *size = v ? (char *)get_attribute(v,"size") : NULL; 
	long nsize = size ? atoi(size) : -1 ;
	return nsize;
    }
    else if(spn->tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)spn->nodeptr;
	return opn ? LOGR_get_ip_sizebytes(opn) : -1;
    }
    return -1;
}

int
LOGR_get_simple_num(WebSimplePagePtr spn)
{
    if(!spn)
	return -1;

    if(spn->tp == PWEBNODE)
    {
	PNODE v = (PNODE)spn->nodeptr;
	return  v ? get_vertex_number(v) : -1;
    }
    else if(spn->tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)spn->nodeptr;
	return opn ? LOGR_get_ip_num(opn) : -1;
    }
    return -1;
}

long
LOGR_get_simple_naccesses(WebSimplePagePtr spn)
{
    if(!spn)
	return -1;


    if(spn->tp == PWEBNODE)
    {
	PNODE v = (PNODE)spn->nodeptr;
	return  v ? (long)get_counter(v) : -1;
    }
    else if(spn->tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)spn->nodeptr;
	return opn ? LOGR_get_ip_naccesses(opn) : -1;
    }
    return -1;
}

const char *
LOGR_get_simple_title(WebSimplePagePtr spn)
{
    if(!spn)
	return NULL;
    if(spn->tp == PWEBNODE)
    {
	PNODE v = (PNODE)spn->nodeptr;
	return  v ? get_attribute(v,"title"): NULL;
    }
    else if(spn->tp == PWEBINDEPENDENT)
    {
	return NULL;
    }
    return NULL;
}

const char *
LOGR_get_simple_mime(WebSimplePagePtr spn)
{
    if(!spn)
	return NULL;

    if(spn->tp == PWEBNODE)
    {
	PNODE v = (PNODE)spn->nodeptr;
	return  v ? get_attribute(v,"mime"): NULL;
    }
    else if(spn->tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)spn->nodeptr;
	return opn ? LOGR_get_ip_mime(opn) : NULL;
    }
    return NULL;
}


short
LOGR_is_simple_page_object(WebSimplePagePtr spn)
{
    if(!spn)
	return 0;

    if(spn->tp == PWEBNODE)
    {
	PNODE v = (PNODE)spn->nodeptr;
	return  v ? is_vertex_object(v) : 0;
    }
    else if(spn->tp == PWEBINDEPENDENT)
    {
	return 0;
    }
    return 0;
}

short
LOGR_is_simple_page_html(WebSimplePagePtr spn)
{
    char *mime = (char *)LOGR_get_simple_mime(spn);
    if(!spn)
	return 0;
    if(spn->tp == PWEBNODE)
    {
	if(!mime)
	    mime = (char *)HTGetMime((char *)LOGR_get_simple_url(spn));
    }
    return (mime && (!strcmp(mime,"www/unknown") 
		     || !strcmp(mime,"text/html")));

}

short
LOGR_is_simple_page_independent(WebSimplePagePtr spn)
{
    if(!spn)	
	return 0;
    return (spn->tp == PWEBINDEPENDENT);
}

